/**
 * @Date: Mar 8, 2010 8:07:06 PM
 */
package com.philip.journal.home.controller.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import com.philip.journal.common.StringUtils;
import com.philip.journal.core.controller.ResponseHandler;
import com.philip.journal.core.service.ServiceProxy;
import com.philip.journal.home.controller.HomeFieldname;

/**
 * @author cry30
 */
public class RenameNodeAction extends AbstractEntryAction {

    /**
     * Default delegate constructor.
     *
     * @param serviceProxy Service proxy to be injected by Spring.
     * @param responseHandler Response handler to be injected by Spring.
     */
    public RenameNodeAction(final ServiceProxy serviceProxy, final ResponseHandler responseHandler) {
        super(serviceProxy, responseHandler);
    }

    @Override
    public void execute() throws ServletException, IOException
    {
        String title = getHttpServletRequest().getParameter(TITLE);
        final String node = getHttpServletRequest().getParameter(NODE_ID);
        final String leaf = getHttpServletRequest().getParameter(IS_LEAF);

        title = getStringUtils().removeHtmlTags(title);
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put(HomeFieldname.TITLE, title);

        final boolean isLeaf = Boolean.parseBoolean(leaf);
        final long nodeId = parseId(node);
        if (isLeaf) {
            getServiceProxy().renameEntry(nodeId, title);
        } else {
            getServiceProxy().renameBranch(nodeId, title);
        }
        getResponseHandler().respondSuccess(getHttpServletResponse(), map);
    }

    StringUtils getStringUtils() {
        return StringUtils.getInstance();
    }

}
